home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / GRAPH_FO / (GRAPH / GRAPH_SO / GRVERTEX.C < prev   
Text File  |  1991-04-03  |  4KB  |  155 lines

  1. /******************************************************************************
  2.     GrVertex.c
  3.         Graph methods in Object C.
  4.         This implements the GrVertex.
  5.  
  6.     SUPERCLASS = GrNode
  7.  
  8.     Copyright ⌐ 1991 Maarten Meijer. All rights reserved.
  9.         CIS 100016,1764; FidoNet 2:512/114
  10. *******************************************************************************/
  11.  
  12. long    gBlokNumber = 1L;
  13.  
  14. #define kLeftSize        20    /* offsets from center */
  15. #define kRightSize        20
  16. #define kTopSize        10
  17. #define kBottomSize        10
  18.  
  19. #define FRAMESHAPE        FrameRect
  20. #define ERASESHAPE        EraseRect
  21. #define HILITESHAPE        InvertRect
  22.  
  23. #include    "GrVertex.h"
  24.  
  25. /* GrVertex methods ********************************************************/
  26.  
  27. /******************************************************************************
  28.     IGraphNode
  29.  
  30.         initialize a vertex
  31. *******************************************************************************/
  32. void
  33. GrVertex::IGraphNode() {        /* OVERRIDE */
  34.     inherited::IGraphNode();
  35.     center.h = center.v = 0;
  36.     blokno = gBlokNumber++;
  37.     }
  38.  
  39. /******************************************************************************
  40.     _Draw
  41.  
  42.         Draws the vertex outline, used in Draw and SetRegion
  43. *******************************************************************************/
  44. void
  45. GrVertex::_Draw() {
  46.     Rect    theRect;
  47.  
  48.     GetRect(&theRect);
  49.     FRAMESHAPE(&theRect);
  50.     }
  51.  
  52. /******************************************************************************
  53.     Draw
  54.  
  55.         Erase and redraw the vertex
  56. *******************************************************************************/
  57. void
  58. GrVertex::Draw() {
  59.     Rect        theRect;
  60.  
  61.     Str255        number;
  62.     short        width, height;
  63.     FontInfo    info;
  64.  
  65.     GetRect(&theRect);
  66.     ERASESHAPE(&theRect);
  67.     _Draw();
  68.  
  69.     NumToString(blokno, number);
  70.     width = StringWidth(number);
  71.     GetFontInfo(&info);
  72.     height = info.ascent;
  73.  
  74.     MoveTo(center.h - (width>>1), center.v - (height >> 1) + height );
  75.     DrawString(number);
  76.  
  77.     if(selected) {    /* this can be modified */
  78.         GetRect(&theRect);    /* this is doubled from _Draw */
  79.         InsetRect(&theRect, 1, 1);
  80.         HILITESHAPE(&theRect);
  81.         }
  82.     }
  83.  
  84. /******************************************************************************
  85.     SetRegion
  86.  
  87.         Set the hot region of the vertex.
  88. *******************************************************************************/
  89. void
  90. GrVertex::SetRegion() {
  91.     Rect    theRect;
  92.  
  93.     inherited::SetRegion();
  94.     OpenRgn();
  95.     _Draw();
  96.     CloseRgn(hotRegion);
  97.     }
  98.  
  99. /******************************************************************************
  100.     SetCenter
  101.  
  102.         Set the center of the vertex and redraw it
  103. *******************************************************************************/
  104. void
  105. GrVertex::SetCenter(Point thePoint) {
  106.     center = thePoint;
  107.     Draw();
  108.     SetRegion();
  109.     }
  110.  
  111. /******************************************************************************
  112.     GetCenter
  113.  
  114.         return the center of the vertex.
  115. *******************************************************************************/
  116. Point
  117. GrVertex::GetCenter(void) {
  118.     return (center);
  119.     }
  120.  
  121. /******************************************************************************
  122.     GetRect
  123.  
  124.         Return the rect that encloses the Vertex. This is the ONLY method
  125.         that defines the enclosing rect.
  126. *******************************************************************************/
  127. void
  128. GrVertex::GetRect(Rect *r) {
  129.     Rect area;
  130.     area.top = center.v - kTopSize;
  131.     area.left = center.h - kLeftSize;
  132.     area.bottom = center.v + kBottomSize;
  133.     area.right = center.h + kRightSize;
  134.     *r = area;
  135.     }
  136.  
  137. /******************************************************************************
  138.     NodeInRect
  139.  
  140.         return true if the Vertex needs to be redrawn if the rect
  141.         is changed.
  142. *******************************************************************************/
  143. Boolean
  144. GrVertex::NodeInRect(Rect *r) {
  145.     Rect area = *r;
  146.  
  147.     area.top -= kTopSize;
  148.     area.left -= kLeftSize;
  149.     area.right += kRightSize;
  150.     area.bottom += kBottomSize;
  151.  
  152.     return PtInRect(center, &area);
  153.     }
  154.  
  155.